﻿using MediatR;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Domains.Setup;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.AddWeighingSheet
{
    public class AddWeighingSheetHandler : IRequestHandler<AddWeighingSheetCommand, Result>
    {
        IBaseRepository<Slaughter> SlaughterRep;
        IBaseRepository<WeighingSheets> WeighingSheetsRep;

        public AddWeighingSheetHandler(IBaseRepository<Slaughter> slaughterRep, IBaseRepository<WeighingSheets> weighingSheetsRep)
        {
            SlaughterRep = slaughterRep;
            WeighingSheetsRep = weighingSheetsRep;
        }
        /// <summary>
        /// 副产称重添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>AddWeighingSheetCommand
        /// <returns></returns>
        public async Task<Result> Handle(AddWeighingSheetCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var ids = SlaughterRep.GetAll().FirstOrDefault(x => x.SlaughterNo == request.ProjectNo);
                var WeighingSheetsRes = WeighingSheetsRep.Add(new WeighingSheets()
                {
                    WeighDate = DateTime.Now,
                    ProjectNo = request.ProjectNo,
                    DepotId = request.DepotId,
                    SkinWeight = request.SkinWeight,
                    GrossWeight = request.GrossWeight,
                    SlaughterIds = ids.SlaughterId.ToString(),
                    NetWeight = request.NetWeight,
                    WeighingSheetsNum = request.WeighingSheetsNum,
                    WeighingDesc = request.WeighingDesc,
                    IsDel = 0,
                    GoodsMinuteId = request.GoodsMinuteId,
                });
                if (WeighingSheetsRes > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = null;
                    result.Msg = "添加成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}

